// pages/video/video.js
const app = getApp();
const utils = require("../../utils/util");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url_list: [],
    search:"",
    community_id:"",
    show_list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      community_id:options.id
    })
    this.getList();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarColor({
      backgroundColor: '#8fd3f4',
      frontColor: '#ffffff',
    })
  },

  getList(){
    this.setData({
      url_list:[]
    });
    this.getVideoList(this.data.community_id).then(res => {
      if (res.length > 0) {
        wx.setStorageSync('video_appkey', res[0].appkey);
        wx.setStorageSync('video_secret', res[0].secret);
        for (let item of res) {
          this.getVideoUrl(item.deviceserial,item.ezviz_name);
        }
      }
    });
  },
  getSearchList(e){
    let reg = new RegExp(e.detail, "i");
    let search_list = [];
    for(let item of this.data.url_list){
      if(item.name.match(reg)){
        search_list.push(item);
      }
    }
    this.setData({
      show_list:search_list
    })
  },

  setVideoToken() {
    return new Promise(require => {
      const formdata = {
        'appKey': wx.getStorageSync('video_appkey'),
        'appSecret': wx.getStorageSync('video_secret')
      };
      wx.request({
        url: 'https://open.ys7.com/api/lapp/token/get',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: utils.JSON_to_URLEncoded(formdata),
        success: res => {
          if (res.data.code == 200) {
            wx.setStorage({
              key: 'video_token',
              data: res.data.data.accessToken,
              success: () => {
                require(true);
              }
            });
          }
        }
      })
    })

  },
  // 获取直播地址，需要通道号
  getVideoUrl(deviceSerial,device_name) {
    const formdata = {
      accessToken: wx.getStorageSync('video_token'),
      deviceSerial: deviceSerial,
      protocol: 2
    }
    wx.request({
      url: 'https://open.ys7.com/api/lapp/v2/live/address/get',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: utils.JSON_to_URLEncoded(formdata),
      success: res => {
        if (res.data.code == "200") {
          let url_item={name:device_name,...res.data.data};
          this.setData({
            url_list: this.data.url_list.concat(url_item),
            show_list:this.data.url_list.concat(url_item)
          })
        } else if (res.data.code == "10002" || res.data.code == "10001") {
          this.setVideoToken().then(() => {
            this.getVideoUrl(deviceSerial,device_name);
          });
        }
      }
    })
  },
  // 获取直播视频列表
  getVideoList(id) {
    return new Promise(resolve => {
      wx.request({
        url: 'https://api.liandunkaoqin.com/api/HIKapi/dispatcher',
        method: "POST",
        header: {
          "Authorization": app.globalData.token
        },
        data: {
          "action": "list_ezviz",
          "openid": app.globalData.unionid,
          "zone_id": id
        },
        success: res => {
          if (res.statusCode == 401) {
            app.login().then(res => {
              app.globalData.token = res;
              this.getVideoList();
            });
          } else {
            if (res.data.ret == 0) {
              resolve(res.data.data);
            }
          }
        }
      })
    })

  }

})